import pytest,time
import logging
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

import json
def read_json(file_path):
    with open(file_path, "r") as f:
        data = json.load(f)
        return [(item["username"], item["password"]) for item in data]
@pytest.fixture(scope="class")
def driver():
    logging.debug('--------start.case----------------')
    driver=webdriver.Firefox()
    driver.get('https://list.jiuxian.com/')
    driver.maximize_window()
    time.sleep(3)
    yield driver
    logging.debug('--------start.case----------------')
    time.sleep(3)
    driver.quit()
@pytest.mark.parametrize("username,passowrd",read_json('D:\gao1\PythonProject4\Lx\jsonc.json'))
def test_jiuxian_login(driver,username,passowrd):
    logging.debug('--------start.case----------------')
    #点击去登录
    driver.find_element(By.XPATH,'/html/body/div[2]/div/div[1]/div/a[1]').click()
    time.sleep(3)
    #断言1：账号登录
    dy1=driver.find_element(By.XPATH,'/html/body/div/form/div/div/div/div/div[1]/div[1]/a[1]').text
    print("断言1：",dy1)
    #进入窗口
    # driver.switch_to.frame(driver.find_element(By.XPATH,'//iframe[start-with(@id,"x-URS-iframe")]'))
    # driver.switch_to.frame(driver.find_element(By.XPATH, '//iframe[contains(@id,"-URS-iframe")]'))
    # driver.switch_to.frame(driver.find_element(By.XPATH, '//iframe[@id："x-URS-iframe"]'))
    # time.sleep(2)
    #输入账号密码
    driver.find_element(By.NAME,'uname').send_keys(username)
    driver.find_element(By.ID,'password').send_keys(passowrd)
    # time.sleep(10)
    # #点击登录按钮
    # driver.find_element(By.ID,'save').click()
    # time.sleep(3)
    # #断言2：
    # dy2=driver.find_element(By.XPATH,'/html/body/div[2]/div/div[1]/div/a[1]').text
    # print("断言2：",dy2)
    logging.debug('--------------case.end----------------')
def test_jiuxian_sousuo(driver):
    test_jiuxian_login(driver)
    #搜索框输入内容
    a=driver.find_element(By.ID,'wd')
    a.send_keys('五粮液')
    a.send_keys(Keys.ENTER)
    time.sleep(3)
    dy3=driver.find_element(By.XPATH,'/html/body/div[6]/div/div[2]/div[1]/div[2]/a[2]').text
    print("断言3：",dy3)
    driver.implicitly_wait(10)
    #进入商品详情
    a=driver.find_element(By.XPATH,'/html/body/div[6]/div/div[2]/div[5]/ul/li[1]/div[2]/div[3]/a')
    ActionChains(driver).click(a).perform()
    time.sleep(3)
    #切换窗口
    driver.switch_to.window(driver.window_handles[-1])
    time.sleep(3)
    dy4 = driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div[2]/div[1]/h1').text
    print("断言4：", dy4)
    #点击加入购物车
    a=WebDriverWait(driver,40,0.5).until(EC.presence_of_element_located((By.ID,'addToCartForDetail')))
    a.click()
    # driver.find_element(By.ID,'addToCartForDetail').click()
    time.sleep(3)
    driver.refresh()

if __name__ == '__main__':
    pytest.main()